package com.guet.careapp.utils;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Convertor {

//  public static void main(String[] args) {
//    Class<?> fieldType = getFieldType(Role.class, "creator");
//    System.out.println(fieldType.getName());
//  }

  public static Class<?> getFieldType(Class<?> clazz, String fieldName) {
    Field[] fields = clazz.getFields();
    Class<?> type = null;
    List<Field> listFiled = getClassAndSuperField(clazz);
    for (Field field : listFiled) {
      if (field.getName().equals(fieldName)) {
        type = field.getType();
      }
    }
    return type;
  }

  /**
   * 获得类字段（包括父类）
   */
  public static List<Field> getClassAndSuperField(Class<?> clazz) {
    Field[] fields = clazz.getDeclaredFields();
    List<Field> result = new ArrayList<Field>();
    if (clazz.getSuperclass() != null) {
      result.addAll(getClassAndSuperField(clazz.getSuperclass()));
    }
    for (Field field : fields) {
      result.add(field);
    }
    return result;
  }

  public static List<StringMap> MapToList(Map<String, Object> map) {
    List<StringMap> resultList = new ArrayList<StringMap>();
    for (String key : map.keySet()) {
      StringMap stringMap = new StringMap(key, map.get(key));
      resultList.add(stringMap);
    }
    return resultList;
  }
}
